package bg.code.commons.plugin.ui.model;

import bg.code.commons.builder.vo.ColumnDescriptor;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/20
 * 描述：
 */
public class FieldTableModel extends AbstractTableModel {

    private final List<ColumnDescriptor> javaFields;
    private final Object[][] columns;
    private static final List<MethodTableModel.TableHeader> TABLE_HEADERS = new ArrayList<>(30);
    private static final IEditTableCellCallback EMPTY_EDIT_TABLE_CELL_CALLBACK = new EmptyEditTableCellCallback();

    static {
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("序号", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("JAVA字段名称", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("主键", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("字段描述", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("数据库字段名", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("JAVA字段类型", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("JAVA字段类型包含引用", false, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否日期", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("日期格式", false, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否字典", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("字典Code", false, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否必填必选字段", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否单行文本框", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否下拉框", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否多选下拉框", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否多行文本框", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("多行文本框宽", false, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("多行文本框列高", false, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否作为查询项", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否百度UE4", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否附件类型", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是否是多附件类型", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("附件全部是图片", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("限制附件格式", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("前端控件的提示信息", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("前端Label信息", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("文本最大长度", true, (headers, columns, rowIndex, cellIndex) -> {
            return true;
        }));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("是单表关联", true, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("表关联字段", true, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("表关联类", true, EMPTY_EDIT_TABLE_CELL_CALLBACK));
        TABLE_HEADERS.add(new MethodTableModel.TableHeader("表关联类全路径", true, EMPTY_EDIT_TABLE_CELL_CALLBACK));
    }

    public FieldTableModel(List<ColumnDescriptor> javaFields) {
        this.javaFields = javaFields;
        this.columns = new Object[javaFields.size()][TABLE_HEADERS.size()];
        int index = 0;
        for (ColumnDescriptor vo : javaFields) {
            columns[index] = new Object[]{
                    index + 1,
                    vo.getName(),
                    vo.isPrimaryKey(),
                    vo.getComment(),
                    vo.getColumnName(),
                    vo.getType(),
                    vo.getFullType(),
                    vo.isDate(),
                    vo.getDateFormat(),
                    vo.isDict(), vo.getDictCode(),
                    vo.isRequired(),
                    vo.isSingleInput(), vo.isSingleSelect(), vo.isMultipleSelect(),
                    vo.isMultipleInput(), vo.getMultipleInputWidth(), vo.getMultipleInputHeight(), vo.isQueryState(),
                    vo.isBaiDuUe4(),
                    vo.isAttachment(), vo.isAttachmentMultiple(), vo.isAttachmentAllIsImg(), vo.getAttachSuffix(),
                    vo.getPlaceholder(), vo.getLabelName(), vo.getMaxLength(),
                    vo.isOneOne(),
                    vo.getOneOneField(),
                    vo.getOneOneClassName(),
                    vo.getOneOneClassQualifiedName()
            };
            index++;
        }
    }

    @Override
    public int getRowCount() {
        return columns.length;
    }

    @Override
    public int getColumnCount() {
        return TABLE_HEADERS.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return TABLE_HEADERS.get(columnIndex).getHeaderName();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Object val = getValueAt(0, columnIndex);
        return val == null ? Object.class : val.getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return TABLE_HEADERS.get(columnIndex).isEditState();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < getRowCount() && columnIndex < getColumnCount()) {
            return columns[rowIndex][columnIndex];
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        columns[rowIndex][columnIndex] = aValue;
        fireTableCellUpdated(rowIndex, columnIndex);
    }
}
